package com.hhsjdesign.dynamicui.core.widgetimpl.businesswidget.adapter

import android.content.Context
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.hhsjdesign.dynamicui.utils.BusinessViewCreatorFactory
import com.hhsjdesign.dynamicui.businessdata.IViewable
import com.hhsjdesign.dynamicui.utils.BusinessViewCreator
import com.hhsjdesign.dynamicui.core.property.viewdata.ItemViewData

/**
 * 适配器，
 * 1，context
 * 2，itemWidgetType 用于本地化创建子视图模板
 * 3，businessType 用于确定业务数据类型
 * 4，viewWidget 用于动态化创建子视图模板
 * 5，itemDataList 子视图数据
 * 6，itemWidth，itemHeight，parentHeightMode
 */
open class RecyclerSimpleAdapter//业务类型//用于目前，在不适用ItemViewData的情况下//item视图参数
    (
    val context: Context,
    val businessType: String,//用于目前，在不适用ItemViewData的情况下
    val itemWidgetType: String?,//item视图参数
    val itemViewData: ItemViewData?,
    val itemDataList: MutableList<IViewable<*>>,
    val itemWidth: Int,
    val itemHeight: Int,
    val parentHeightType: Int
) : RecyclerView.Adapter<WidgetHolder<*>>() {

    private val viewHolder: BusinessViewCreator<*>?

    init {
        viewHolder = BusinessViewCreatorFactory.makeViewHolder(this.businessType)
    }

    override fun getItemId(position: Int): Long {
        return itemDataList[position].hashCode().toLong()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): WidgetHolder<*> {
        return WidgetHolder(
            viewHolder!!.createView(
                context,
                itemWidgetType,
                businessType,
                itemViewData,
                parent
            ),
            viewHolder
        )
    }

    override fun onBindViewHolder(holder: WidgetHolder<*>, position: Int) {
        //创建视图
        holder.businessHolder.bindDataAndEvent(
            holder.itemView,
            itemDataList[position],
            itemWidgetType,
            businessType,
            itemViewData
        )

    }

    override fun getItemCount(): Int {
        return itemDataList.size
    }
}